home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Question about system call
- Date: Thu, 18 Jan 96 22:04:48 GMT
- Organization: none
- Message-ID: <822002688snz@genesis.demon.co.uk>
- References: <4dekv8$9ja@spider.hik.se> <ALUN.CHAMPION.96Jan18120927@g7240065.bridge.bst.bls.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <ALUN.CHAMPION.96Jan18120927@g7240065.bridge.bst.bls.com>
- Alun.Champion@bridge.bst.bls.com "Alun Champion" writes:
-
- >: An example:
- >
- >: write(0, (the integer value), sizeof(int));
-
- >1) Try:
- >
- > write(0, &int_value, sizeof(int));
-
- That assumes (the integer value) is an lvalue, not a general expression (or
- even a constant).
-
- >2) Try:
- >
- > char buf[10];
- > sprintf(buf, "%d", int_value);
- >
- > write(0, buf, strlen(buf)-1);
-
- strlen returns the number of characters in the string before the '\0' so
- you don't want the -1. sprintf returns this value anyway so there's no
- need to call strlen.
-
- >A couple of points, this is not portable, why not use the stdio library,
- >this would then become
- >
- >1)
- > printf("%*s", sizeof(int), (char*)&int_value);
-
- No, %s outputs a string i.e. stops if it hits a '\0' which it may well
- do within the internal representation of an integer. Also * reads an
- int argument so you need (int)sizeof(int). A close standard equivalent is:
-
- fwrite(&int_val, sizeof(int), 1, stdout);
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-